package edu.gj.demo1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Simple
 * @date 2020/4/23 14:58
 */
public class Server {


    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;

        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        PrintStream printStream = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;

        try {
            //创建服务  等待链接
            serverSocket = new ServerSocket(8888);
            //链接获取到socket对象
            socket = serverSocket.accept();
            //获取输入输出流
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            printStream = new PrintStream(bufferedOutputStream);
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);

            String info = reader.readLine();
            System.out.println("服务器获取到信息：" + info);

            printStream.println("客户端，你好！");
            printStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStreamReader){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStream){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != printStream){
                printStream.close();
            }
            if (null != bufferedOutputStream){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != outputStream){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != socket){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != serverSocket){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }


}
